package accepttests;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import database.DataseConsult;
import database.rescue.GetUserDatabase;

import sessionManager.SessionManager;
import user.User;
import user.UserIF;
import user.blog.BlogIF;
import user.blog.post.Post;
import user.profile.Profile;

public class AddCommentFacade {
	//private List<UserIF> users;
	private SessionManager session;
	
	public AddCommentFacade() {
		session = SessionManager.getInstance();
	}
	
	public void loadData() throws Exception {
		//users = GetUserDatabase.getallUsers();
	}
	
	public void createProfile(String login, String password, String nome_exibicao,
			String email, String sexo, String dataNasc, String endereco,
			String interesses, String quem_sou_eu, String filmes,
			String musicas, String livros) throws Exception {
		
		Profile profile = new Profile(login, password, nome_exibicao, email, sexo,
				dataNasc, endereco, interesses, quem_sou_eu, filmes, musicas,
				livros);
		
		UserIF user = new User(login, password, profile);
		user.save();
		// TODO
	}
	
	public String logon(String login, String password) throws Exception {
		return session.loginUser(login, password);
	}
	
	public String getBlogByLogin(String login, Integer index) throws Exception {
		return session.getBlogByLogin(login, index);
		/*while(itBlogs.hasNext()) {
			UserIF user = itBlogs.next();
			if(login.equals(user.getLogin())) {
				return user.getBlogByIndex(index).getBlogID();
			}
		}
		
		return null;*/
	}
	
	public String getPost(String blogId, Integer index) throws Exception {
		return session.getBlogByBID(blogId).getAllPosts().get(index).getPostID();
	}
	
	private List<BlogIF> getAllBlogs() {
		/*Iterator<UserIF> itUsers = users.iterator();
		List<BlogIF> allBlogs = new LinkedList<BlogIF>();
		while(itUsers.hasNext()) {
			UserIF user = itUsers.next();
			Iterator<BlogIF> itBlogs = user.getBlogs().iterator();
			while(itBlogs.hasNext()) {
				allBlogs.add(itBlogs.next());
			}
		}
		return allBlogs;*/
		return session.getAllBlogs();
	}
	
	public List<Post> getAllPosts() throws Exception {
		Iterator<BlogIF> itBlogs = getAllBlogs().iterator();
		List<Post> allPosts = new LinkedList<Post>();
		while(itBlogs.hasNext()) {
			Iterator<Post> itPosts = itBlogs.next().getAllPosts().iterator();
			while(itPosts.hasNext()) {
				allPosts.add(itPosts.next());
			}
		}
		return session.getAllPosts();
	}
	
	public String addComment(String sessionId, String postId, String text) throws Exception {
		String tg = session.getPostByPID(postId).getIdSession();
		String login = DataseConsult.getLoginByUID(tg);
		
		GetUserDatabase gud = new GetUserDatabase(login);
		UserIF user = gud.getUser();
		String h = user.getPostByPID(postId).addComment(sessionId, text);
		user.save();
		
		return h;
		//return session.getUserBySessionID(sessionId).getPostByPID(postId).addComment(sessionId, text);
		
		/*Iterator<UserIF> itUsers = session.getUsers().iterator();
		while(itUsers.hasNext()) {
			UserIF user = itUsers.next();
			Iterator<BlogIF> itBlogs = user.getBlogs().iterator();
			while(itBlogs.hasNext()) {
				Iterator<Post> itPosts = itBlogs.next().getAllPosts().iterator();
				while(itPosts.hasNext()) {
					Post post = itPosts.next();
					if(postId.equals(post.getPostID())){
						return post.addComment(sessionId, text);
					}
				}
			}
		}*/
		
		//return null;
	}
	
	public Integer getNumberOfComments(String postId) throws Exception {
		Iterator<Post> itPosts = session.getAllPosts().iterator();
		
		while(itPosts.hasNext()) {
			Post post = itPosts.next();
			System.out.println(post.getComments().getComments().size());
			if(postId.equals(post.getPostID())){
				return post.numberOfComments();
			}
		}
		
		return null;
	}
	
	public String getComment(String postId, Integer index) throws Exception {
		Iterator<Post> itPosts = getAllPosts().iterator();
		
		while(itPosts.hasNext()) {
			Post post = itPosts.next();
			if(postId.equals(post.getPostID())){
				return post.getComments().getComment(index);
			}
		}
		
		return null;
	}
	
	public String getCommentText(String idComment) throws Exception {
		Iterator<Post> itPosts = getAllPosts().iterator();
		
		while(itPosts.hasNext()) {
			Post post = itPosts.next();
			if( post.getComments().getCommentByCID(idComment) != null) {
				return post.getComments().getCommentByCID(idComment);
			}
		}
		
		return null;
	}
	
	public void logoff(String sessionId) throws Exception {
		session.logoffUser(sessionId);
	}
	
	public void saveData() throws Exception {
		/*Iterator<UserIF> itProfiles = users.iterator();
		PasswdDatabaseManager pdm = PasswdDatabaseManager.getInstance();
		while (itProfiles.hasNext()) {
			UserIF tempProfile = itProfiles.next();
			tempProfile.save();
			pdm.setPasswd(tempProfile.getLogin(), tempProfile.getPersonalDataFromProfile(user.profile.Flags.EMAIL));
		}
		pdm.save();*/
	}
	
	public String getCommentAuthor(String idComment) throws Exception {
		Iterator<Post> itPosts = getAllPosts().iterator();
		
		while(itPosts.hasNext()) {
			Post post = itPosts.next();
			if( post.getComments().getCommentByCID(idComment) != null) {
				return post.getComments().getCommentAuthor(idComment);
			}
		}
		
		return null;
	}
}